/**
 * Created by yihua on 2017/2/19.
 */
$(document).ready(function () {
    initUploadAvatar('loadimg');
    $('#myModal').on('shown.bs.modal', function (e) {
        initParentIds();
        $('#start_time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose: true, todayBtn: true});
        $('#end_time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose: true, todayBtn: true});
    })

    var options = {
        url: '/home/shop/addindexregion',
        beforeSubmit: validate,  // pre-submit callback
        success: showResponse,  // post-submit callback
        dataType: 'json',
        type: 'post',
    };

    $("#confirm").unbind('click').click(function () {
        options.data = {
            'thumb': $('#loadimg').data('url')
        }
        $("#regionForm").ajaxSubmit(options)
    });


});


function validate(formData, jqForm, options) {
    console.log(options);
    for (var i = 0; i < formData.length; i++) {
        if (formData[i].name == 'file') {
            break;
        }
        if (formData[i].name == 'start_time' || formData[i].name == 'end_time') {
            formData[i].value = Date.parse(new Date(formData[i].value)) / 1000;
        }

        if (!formData[i].value) {
            console.log(formData[i].name)
            toastr.error('请输入所有选项', '', {positionClass: 'toast-bottom-center'});
            return false;
        }
    }
}

function showResponse(responseText, statusText, xhr, $form) {
    console.log(responseText);
    if (0 !== responseText.errno) {
        toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
    } else {
        toastr.success('成功', '', {positionClass: 'toast-bottom-center'});
        $('#myModal').modal("hide");
        window.location.reload();
    }
}
function initParentIds() {
    $.ajax({
        url: '/home/shop/getregioninfo',
        timeout: 100000,
        type: 'get',
        dataType: 'json',
        data: {},
        success: function (rs) {
            if (rs.errno != 0) {
                toastr.error(rs.errno);
                return false;
            } else {
                var data = [{'id': 0, 'text': '无'}];
                for (var i = 0; i < rs.data.length; i++) {
                    data.push({'id': rs.data[i].rid, 'text': rs.data[i].name})
                }
                $("#parent_id").select2({
                    dropdownParent: $("#myModal"),
                    data: data
                })
            }
        },
        error: function () {
            toastr.error('系统错误');
        }
    });
}

function initUploadAvatar(imgId) {
    var id = "#" + imgId;
    $(id).fileinput({
        uploadUrl: "/common/uploadimage", // server upload action
        showRemove: false,
        showUpload: false,
        uploadAsync: true,
        showPreview: false,
        allowedFileExtensions: ['jpg', 'png', 'gif'],
        maxFileCount: 5,
    }).on('fileuploaded', function (event, data, id, index) {
        var ret = data.response;
        if (0 != ret.errno) {
            toastr.error('头像图片', '', {positionClass: 'toast-bottom-center'});
            uploadFlag = false;
        } else {
            var url = ret.data.url;
            $('#loadimg').data('url', ret.data.url);
            //console.log($('#loadimg').data('url'))
            ///console.log(id.data('url'))
        }
        console.log('upload' + imgId + 'OK ', url)
    }).on("filebatchselected", function (event, files) {
        $(id).fileinput("upload");
    });
}